<template>
    <div id="list">
        <el-button
          v-if="!isRead && dataType === 2 && role == 4"
          class="float-right"
          @click="copyPreAll()"
        style="margin-bottom:15px"
        >复制全部</el-button>
       
        <el-table :data="courseTables" :height="tableHeight" :span-method="arraySpanMethod" border tooltip-effect="light" style="width: 100%">
             
            <el-table-column label="Course No" align="left" width="130">
                <template slot-scope="props">
                    <div v-if="props.row.isCategoryRow">
                        <span v-if="role == 4 || dataType == 1">
                            <span v-if="!isRead && props.row.showDivideBtn" class="float-left">
                                <el-button type="text" style="font-weight: 900;font-size: 14px;" @click="showDivideModule()">分组</el-button>
                                <!-- <span>{{ props.row.sequence }}{{ props.row.categoryName }}</span> -->
                            </span>
                        </span>
                        <span v-if="role == 3 && dataType == 2 && props.row.xid != 1066 && props.row.xid != 1577">
                            <span v-if="!isRead && props.row.showDivideBtn" class="float-left">
                                <el-button type="text" style="font-weight: 900;font-size: 14px;" @click="showDivideModule()">分组</el-button>
                                <!-- <span>{{ props.row.sequence }}{{ props.row.categoryName }}</span> -->
                            </span>
                        </span>
                    </div>
                    <div v-if="props.row.isCategoryRow">
                        <div
                            class="float-left"
                            style="display:inline-block;height:34px;line-height:34px;"
                        >
                            <i v-if="props.row.directionCode" style="margin-left:20px;">&nbsp;</i>
                            <span>{{ props.row.sequence }}{{ props.row.categoryNameEn }}</span>
                        </div>
                        <div class="float-left" style="margin-left:40px;" v-if="!isRead">
                            <!-- <el-button
                                type="text"
                                @click="add(props.$index)"
                                v-if="props.row.showAddCourseBtn"
                            >新增课程</el-button> -->
                            <el-button
                                type="text"
                                @click="add(props.$index)"
                                v-if="props.row.showAddCourseBtn && dataType === 2 && role == 4"
                            >新增课程</el-button>
                            <el-button
                                type="text"
                                @click="add(props.$index)"
                                v-if="props.row.showAddCourseBtn && dataType === 1"
                            >新增课程</el-button>
                            <template v-if="dataType==2 && props.row.showAddCourseBtn">
                                <el-button
                                    type="text"
                                    @click="selectDirection()"
                                    v-if="!props.row.directionCode && props.row.hasCourse"
                                >分方向</el-button>
                                <el-button
                                    type="text"
                                    @click="deleteDirection(props.$index)"
                                    v-else-if="props.row.directionCode"
                                >删除方向</el-button>
                            </template>
                            <template v-if="props.row.hasCourse">
                                <el-button
                                    type="text"
                                    @click="showDivideModule(props.$index, 1)"
                                >分模块</el-button>
                                <el-button style="font-weight: 900;font-size: 14px;" type="text" @click="showDivideModule(props.$index, 2)">分组</el-button>
                            </template>
                        </div>
                    </div>
                    <div v-else>
                        <el-checkbox
                            v-model="courseTables[props.row.index].isCourseCheck"
                            :key="props.row.keyId"
                            @change="pickCourse(props.row, props.row.keyId, props.row.index, 2)"
                        />
                         
                        <span>{{ props.row.courseCode }}</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column min-width="180" align="center" label="Course Name" show-overflow-tooltip>
                <template slot-scope="props">
                    <a
                        @click="showCourseDetail(props.row)"
                        v-bind:class="{'new-record':inChange && props.row.changeStatus == 1,'edit-record': inChange && props.row.changeStatus == 2,'remove-record': inChange && props.row.changeStatus == 3, }"
                    >{{ props.row.courseNameEn }}</a>
                </template>
            </el-table-column>
            <el-table-column label="Credit" align="center" width="64">
                <template slot-scope="props">{{ props.row.studyScore }}</template>
            </el-table-column>
            <el-table-column label="Total Period" align="center" width="110">
                <template slot-scope="props">{{ props.row.studyTime }}</template>
            </el-table-column>
            <el-table-column label="Semester" align="center" width="150">
                <template slot-scope="props">
                    <span v-if="props.row.term == '春秋季'">Spring And Autumn</span>
                    <span v-else-if="props.row.term == '春季'">Spring</span>
                    <span v-else-if="props.row.term == '秋季'">Autumn</span>
                </template>
            </el-table-column>
            <el-table-column label="Compulsorys" align="center" width="120">
                <template slot-scope="props">
                    <el-select
                        v-if="props.row.isEdit"
                        v-model="props.row.compulsorys"
                        style="width:100%"
                    >
                        <el-option label="yes" value="1"/>
                        <el-option label="no" value="0"/>
                    </el-select>
                    <span
                        v-if="!props.row.isEdit"
                    >{{ props.row.compulsory == '' || props.row.compulsory == null ? '' : props.row.compulsory == 0 ? 'No' : 'Yes' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="Pick a few" align="center" show-overflow-tooltip width="100" style="position:relative;">
                <template slot-scope="props">
                    <span><pre>{{ props.row.parentRemark }}</pre></span>
                    <template v-if="role == 4 || dataType == 1">
                        <i
                            v-if="props.row.parentId && !isRead"
                            class="el-icon-delete"
                            style="color:red;cursor:pointer;position:absolute;right:4px;top:50%;margin-top:-6px;"
                            title="删除课程分组"
                            @click="deleteGroup(props.row.parentId,props.row)"
                        ></i>
                    </template>
                    <template v-if="role == 3 && dataType == 2 && props.row.categoryId != 1066 && props.row.categoryId != 1577">
                        <i
                            v-if="props.row.parentId && !isRead"
                            class="el-icon-delete"
                            style="color:red;cursor:pointer;position:absolute;right:4px;top:50%;margin-top:-6px;"
                            title="删除课程分组"
                            @click="deleteGroup(props.row.parentId,props.row)"
                        ></i>
                    </template>
                </template>
            </el-table-column>
            <el-table-column label="Remark" min-width="130" align="center" show-overflow-tooltip>
                <template slot-scope="props">
                    <div v-if="!props.row.isEdit">
                        {{ props.row.remarksEn }}
                    </div>
                    <el-input v-if="props.row.isEdit" v-model="props.row.remarks"></el-input>
                </template>
            </el-table-column>
            <el-table-column v-if="!isRead" label="Operating"  width="130" align="center">
                <template slot-scope="props">
                    <template v-if="role == 4">
                    <!-- <el-button
                        v-if="props.row.isAddRow"
                        type="text"
                        size="mini"
                        @click="add(props.row)"
                    >新增</el-button> -->
                    <el-button
                        v-if="props.row.isAddRow"
                        type="text"
                        size="mini"
                        @click="add(props.row)"
                    >新增</el-button>
                    <!-- <el-button
                        v-if="props.row.isAddRow && dataType === 2 "
                        type="text"
                        size="mini"
                        @click="copyPre(props.row)"
                    >复制之前</el-button> -->
                    <el-button
                        v-if="props.row.isAddRow && dataType == 2"
                        type="text"
                        size="mini"
                        @click="copyPre(props.row)"
                    >复制之前</el-button>
                    <el-button
                        v-if="props.row.isCourseRow && !props.row.isEdit"
                        type="text"
                        size="mini"
                        @click="edit(props.row)"
                    >编辑</el-button>
                    <el-button
                        v-if="props.row.isCourseRow && !props.row.isEdit"
                        type="text"
                        size="mini"
                        @click="remove(props.row)"
                    >删除</el-button>
                    <el-button
                        v-if="props.row.isCourseRow && props.row.isEdit"
                        type="text"
                        size="mini"
                        @click="confirmSaveDialog(props.row)"
                    >保存</el-button>
                    <el-button
                        v-if="props.row.isCourseRow && props.row.isEdit"
                        type="text"
                        size="mini"
                        @click="confirmCancelDialog(props.row)"
                    >取消</el-button>
                </template>
                <template v-else>
                    <el-button
                        v-if="props.row.isAddRow && dataType == 2 && props.row.categoryId != 1066 && props.row.categoryId != 1577"
                        type="text"
                        size="mini"
                        @click="add(props.row)"
                    >新增</el-button>
                    <el-button
                        v-else-if="props.row.isAddRow && dataType == 1"
                        type="text"
                        size="mini"
                        @click="add(props.row)"
                    >新增</el-button>
                    <el-button
                        v-if="props.row.isAddRow && dataType == 2 && props.row.categoryId != 1067 && props.row.categoryId != 1578 && props.row.categoryId != 1066 && props.row.categoryId != 1577"
                        type="text"
                        size="mini"
                        @click="copyPre(props.row)"
                    >复制之前</el-button>
                    <template v-if="dataType == 2">
                        <el-button
                            v-if="props.row.isCourseRow && !props.row.isEdit  && props.row.categoryId != 1066 && props.row.categoryId != 1577"
                            type="text"
                            size="mini"
                            @click="edit(props.row)"
                        >编辑</el-button>
                        <el-button
                            v-if="props.row.isCourseRow && !props.row.isEdit && props.row.categoryId != 1066 && props.row.categoryId != 1577"
                            type="text"
                            size="mini"
                            @click="remove(props.row)"
                        >删除</el-button>
                        <el-button
                            v-if="props.row.isCourseRow && props.row.isEdit && props.row.categoryId != 1066 && props.row.categoryId != 1577"
                            type="text"
                            size="mini"
                            @click="confirmSaveDialog(props.row)"
                        >保存</el-button>
                        <el-button
                            v-if="props.row.isCourseRow && props.row.isEdit && props.row.categoryId != 1066 && props.row.categoryId != 1577"
                            type="text"
                            size="mini"
                            @click="confirmCancelDialog(props.row)"
                        >取消</el-button>
                    </template>
                    <template v-if="dataType == 1">
                        <el-button
                            v-if="props.row.isCourseRow && !props.row.isEdit"
                            type="text"
                            size="mini"
                            @click="edit(props.row)"
                        >编辑</el-button>
                        <el-button
                            v-if="props.row.isCourseRow && !props.row.isEdit"
                            type="text"
                            size="mini"
                            @click="remove(props.row)"
                        >删除</el-button>
                        <el-button
                            v-if="props.row.isCourseRow && props.row.isEdit"
                            type="text"
                            size="mini"
                            @click="confirmSaveDialog(props.row)"
                        >保存</el-button>
                        <el-button
                            v-if="props.row.isCourseRow && props.row.isEdit"
                            type="text"
                            size="mini"
                            @click="confirmCancelDialog(props.row)"
                        >取消</el-button>
                    </template>
                </template>
                </template>
            </el-table-column>
        </el-table>
        <el-row v-if="next" class="pd-12px float-right" style="padding-right: 0;">
            <el-col :span="24">
                <el-button size="medium" @click="backStep">上一步</el-button>
                <el-button size="medium" type="primary" @click="nextStep">下一步</el-button>
            </el-col>
        </el-row>
        <el-dialog :visible.sync="showDivideModuleDialog" title="课程编组" width="78%" :close-on-click-modal="false"
            :close-on-press-escape="false">
            <el-card shadow="never" body-style="padding: 24px;">
                <el-table :data="curCheck" stripe size="mini">
                    <el-table-column label="课程代码" prop="courseCode"/>
                    <el-table-column label="课程名称" prop="courseName"/>
                    <el-table-column label="学分" prop="studyScore"/>
                    <el-table-column label="总学时" prop="studyTime"/>
                    <el-table-column label="开课学期" prop="term"/>
                    <el-table-column label="是否必修" align="center" width="72">
                        <template
                            slot-scope="props"
                        >{{ props.row.compulsory == '' || props.row.compulsory == null ? '' : props.row.compulsory == 0 ? '选修' : '必修' }}</template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remarks"/>
                </el-table>
            </el-card>
            <div class="edu-page-title mg-top">规则设置</div>
            <el-card v-loading="loading" shadow="never" body-style="padding: 24px;">
                <el-form
                    ref="divideModuleForm"
                    :inline="true"
                    :model="divideModuleForm"
                    label-width="80px"
                    label-position="left"
                >
                    <el-row :gutter="8">
                        <el-col :span="8">
                            <el-form-item label="课程组名" prop="remark">
                                <el-input v-model="divideModuleForm.remark" placeholder/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="限制" prop="type">
                                <el-radio-group v-model="divideModuleForm.type">
                                    <el-radio :label="0">门数限制</el-radio>
                                    <el-radio :label="1">学分限制</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="2">
              <el-form-item v-if="divideModuleForm.type === 0" label prop="minNum">
                <el-select v-model="divideModuleForm.minNum" placeholder="请选择">
                  <el-option
                    v-for="(item,index) in curCheck"
                    :key="item"
                    :label="index + 1"
                    :value="index + 1"
                  />
                </el-select>
              </el-form-item>
                        </el-col>-->
                        <el-col :span="4">
                            <el-form-item label prop="expression">
                                <el-select v-model="divideModuleForm.expression" placeholder="请选择">
                                    <el-option label="大于等于" value="1"/>
                                    <el-option label="等于" value="2"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="2">
                            <el-form-item v-if="divideModuleForm.type === 0" label prop="maxNum">
                                <el-select v-model="divideModuleForm.maxNum" placeholder="请选择" @change="changeGroupName">
                                    <el-option label="0" value="0" />
                                    <el-option
                                        v-for="(item,index) in curCheck"
                                        :key="item"
                                        :label="index + 1"
                                        :value="index + 1"
                                    />
                                </el-select>
                            </el-form-item>
                            <el-form-item
                                v-if="divideModuleForm.type === 1"
                                label
                                prop="minCredits"
                            >
                                <el-input v-model="divideModuleForm.minCredits" placeholder/>
                            </el-form-item>
                        </el-col> -->
                         <el-col :span="2">
                            <el-form-item v-if="divideModuleForm.type === 0" label prop="maxNum">
                                <el-select v-model="divideModuleForm.minNum" placeholder="请选择" @change="changeGroupName">
                                    <el-option
                                        v-for="(item,index) in curCheck"
                                        :key="item"
                                        :label="index + 1"
                                        :value="index + 1"
                                    />
                                </el-select>
                            </el-form-item>
                            <el-form-item
                                v-if="divideModuleForm.type === 1"
                                label
                                prop="minCredits"
                            >
                                <el-input v-model="divideModuleForm.minCredits" placeholder/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>

            <span slot="footer">
                <div>
                    <el-button @click="showDivideModuleDialog = false">{{ $t('message.cancel') }}</el-button>
                    <el-button
                        type="primary"
                        @click="writeDivideRelation('divideModuleForm')"
                    >{{ $t('message.confirm') }}</el-button>
                </div>
            </span>
        </el-dialog>
        <el-dialog title='复制之前' :visible.sync="copyVisible"   
            width="45%" 
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            @close="resetCopyForm"
            >
            <el-form ref="copyForm" :model="copyForm" label-width="80px" label-position="left" :rules="copyFormRules">
                <el-row :gutter="24">
                    <el-col :span="12">
                        <el-form-item label="年级:" prop="year">
                            <el-date-picker
                                v-model="copyForm.year"
                                value-format="yyyy"
                                type="year"
                                placeholder="选择学年"
                                style="width: 100%"
                                @change="handleInitScheme"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.trainingLevel') + ':'">
                            <el-select v-model="copyForm.trainingLevel" style="width: 100%"
                            clearable @change="handleInitScheme">
                                <el-option
                                    v-for="item in copyOptions.trainingLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.trainingCategory') + ':'">
                            <el-select
                                v-model="copyForm.trainingCategory"
                                style="width: 100%"
                                clearable @change="handleInitScheme"
                                multiple collapse-tags>
                                <el-option
                                    v-for="item in copyOptions.trainingCategory"
                                    :key="item.code"
                                    :label="item.name"
                                    :value="item.code"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.degreeType') + ':'">
                            <el-select v-model="copyForm.degreeType" style="width: 100%;"
                            clearable @change="handleInitScheme">
                                <el-option
                                    v-for="item in copyOptions.degreeType"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.formLearning') + ':'">
                            <el-select
                                v-model="copyForm.formLearning"
                                style="width: 100%"
                                clearable @change="handleInitScheme">
                                <el-option
                                    v-for="item in copyOptions.formLearning"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="培养方案:" prop="schemeId">
                            <el-select
                                v-model="copyForm.schemeId"
                                style="width: 100%"
                                @change="handleChangScheme"
                            >
                                <el-option
                                    v-for="item in copyOptions.schemes"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer">
                <div>
                    <el-button @click="copyVisible = false">{{ $t('message.cancel') }}</el-button>
                    <el-button
                        type="primary"
                        @click="handleSubmitCopy()"
                    >{{ $t('message.confirm') }}</el-button>
                </div>
            </span>
        </el-dialog>

          <el-dialog  title="复制全部" :visible.sync="copyVisibleAll"   
            width="45%" 
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            @close="resetCopyForm"
            >
            <el-form ref="copyForm" :model="copyForm" label-width="80px" label-position="left" :rules="copyFormRules">
                <el-row :gutter="24">
                    <el-col :span="12">
                        <el-form-item label="年级:" prop="year">
                            <el-date-picker
                                v-model="copyForm.year"
                                value-format="yyyy"
                                type="year"
                                placeholder="选择学年"
                                style="width: 100%"
                                @change="handleInitScheme"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.trainingLevel') + ':'">
                            <el-select v-model="copyForm.trainingLevel" style="width: 100%"
                            clearable @change="handleInitScheme">
                                <el-option
                                    v-for="item in copyOptions.trainingLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.trainingCategory') + ':'">
                            <el-select
                                v-model="copyForm.trainingCategory"
                                style="width: 100%"
                                clearable @change="handleInitScheme"
                                multiple collapse-tags>
                                <el-option
                                    v-for="item in copyOptions.trainingCategory"
                                    :key="item.code"
                                    :label="item.name"
                                    :value="item.code"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.degreeType') + ':'">
                            <el-select v-model="copyForm.degreeType" style="width: 100%;"
                            clearable @change="handleInitScheme">
                                <el-option
                                    v-for="item in copyOptions.degreeType"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('culture.scheme.formLearning') + ':'">
                            <el-select
                                v-model="copyForm.formLearning"
                                style="width: 100%"
                                clearable @change="handleInitScheme">
                                <el-option
                                    v-for="item in copyOptions.formLearning"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="培养方案:" prop="schemeId">
                            <el-select
                                v-model="copyForm.schemeId"
                                style="width: 100%"
                                @change="handleChangScheme"
                            >
                                <el-option
                                    v-for="item in copyOptions.schemes"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer">
                <div>
                    <el-button @click="copyVisibleAll = false">{{ $t('message.cancel') }}</el-button>
                    <el-button
                        type="primary"
                        @click="handleSubmitCopyAll()"
                    >{{ $t('message.confirm') }}</el-button>
                </div>
            </span>
        </el-dialog>
        <courseDetail
            :couseDetail="couseDetail"
            :showCourseDialog="showCourseDetailDialog"
            @closeCourseDialog="handlerCloseCourseDialog"
            lang="en"
            v-if="showCourseDetailDialog"
        ></courseDetail>
        <courseList ref="courseDialog" :random-id="randomId"  :training="trainingLevel" :show="showCourseDialog"/>
    </div>
</template>
<script>
import {
    courseLabelRelation,
    addCourse,
    EditCourse,
    deleteCourse,
    getCourseList,
    copyBackCourseLabelRelation,
    copyBackAllCourseLabelRelation,
    findCultureSchemeListByYear,
    getCourseLabelRelGroupMember
} from "common/src/api/courseTemplate";
import {
    deleteCourseLabelRelGroup,
    addCourseLabelRelGroup
} from "common/src/api/culture/cultruescheme";
import { queryDic, findTrainingLevelByProjId, sortTrainingCategories } from "common/src/api/dictionary";
import { getPower } from 'common/src/api/culture/culturePlan'
import UTILS from "common/src/utils/utils";
import Bus from "common/src/utils/bus";
import courseList from "../courseList";
import courseDetail from "common/src/views/culture/common/courseDetail";
export default {
    name: "course-form",
    components: {
        courseList: courseList,
        courseDetail
    },
    props: {
        next: {
            type: Function,
            default: null
        },
        templateId: {
            type: Number,
            default: null
        },
        isRead: {
            type: Boolean,
            default: false
        },
        dataType: {
            type: String,
            default: "1" // 1为模板，2为方案
        },
        projId: {
            type: String,
            default: "1"
        },
        inChange: {
            type: Boolean,
            default: false
        }   
    },
    data() {
        return {
            dialogTitle: "",
            showDivideModuleDialog: false,
            copyVisible: false,
            copyVisibleAll:false,
            divideModuleForm: {
                courseIdList: [],
                chooseNum: "",
                totalNum: "",
                remark: "",
                level: "",
                type: 0,
                minNum: 1,
                maxNum: 1,
                minCredits: 0,
                expression: "1"
            },
            curCheck: [],
            spanDataForGroup: [],
            trainingLevel: "",
            courseCodes: [], // 课程搜索列表
            randomId: "", // 用于事件监听和注销，确保唯一性
            tableHeight: 420,
            rowData: null, // 获取原始数据,包括课程分类和课程列表
            showSaveDialog: false,
            showCourseDialog: false,
            courseTables: [],
            currentRow: null, // 当前操作行,新增/编辑/删除时，保存当前操作行
            options: {
                types: [],
                compulsory: [
                    { value: "0", label: "选修" },
                    { value: "1", label: "必修" }
                ]
            },
            form: {
                keyId: "", // 主键id，用于修改
                courseId: "",
                courseCode: "",
                courseName: "",
                type: "",
                typeName: "",
                studyScore: "",
                studyTime: "",
                pcTime: "",
                experimentTime: "",
                remarks: "",
                term: "",
                compulsory: "0"
            },
            courseRules: {
                courseName: [
                    {
                        required: true,
                        message: "请选择课程",
                        trigger: "blur"
                    }
                ],
                compulsory: [
                    {
                        required: true,
                        message: "请选择是否必修",
                        trigger: "blur"
                    }
                ],
                type: [
                    {
                        required: true,
                        message: "请选择考试方式",
                        trigger: "blur"
                    }
                ],
                studyScore: [
                    {
                        required: true,
                        message: "请填写学分",
                        trigger: "blur"
                    },
                    {
                        validator: this.doubleReg,
                        trigger: "blur"
                    }
                ],
                studyTime: [
                    {
                        required: true,
                        message: "请填写总学时",
                        trigger: "blur"
                    },
                    {
                        validator: this.doubleReg,
                        trigger: "blur"
                    }
                ],
                experimentTime: [
                    {
                        validator: this.doubleReg,
                        trigger: "blur"
                    }
                ],
                pcTime: [
                    {
                        validator: this.doubleReg,
                        trigger: "blur"
                    }
                ]
            },
            selectCoursesData: [],
            copyForm: {
                year: "",
                schemeId: "",
                categoryId: "",
                trainingLevel: "",
                trainingCategory: [],
                degreeType: "",
                formLearning: ""
            },
            copyFormRules: {
                schemeId: {
                    required: true,
                    trigger: 'change',
                    message: '请选择培养方案'
                },
                year: {
                    required: true,
                    trigger: 'blur',
                    message: '请选择年级'
                }
            },
            copyOptions: {
                schemes: [],
                categorys: [],
                trainingLevel: [],
                trainingCategory: [],
                degreeType: [],
                formLearning: []
            },
            copyData: {
                trainingId: this.$route.params.id,
                templateId: "",
                labelId: "",
                year: ""
            },
            showCourseDetailDialog: false,
            couseDetail: {},
            role:""
        };
    },
    watch: {
        showDivideModuleDialog: function(n, o) {
            if (!n) {
                this.init();
                this.curCheck = [];
                this.divideModuleForm.level = "";
                this.divideModuleForm.courseIdList = [];
                this.divideModuleForm.totalNum = "";
                this.divideModuleForm.chooseNum = "";
                this.divideModuleForm.remark = "";
                this.divideModuleForm.minNum = 1;
            }
        },
        copyVisible: function(n, o) {
            if (n) {
                this.copyOptions['trainingLevel'] = [];
                this.copyOptions['trainingCategory'] = [];
                this.copyOptions['degreeType'] = [];
                this.copyOptions['formLearning'] = [];
                this.copyOptions['schemes'] = [];
                this.initCopyOptions();
                this.initTrainingLevelOptions(this.projId);
            }
        },

         copyVisibleAll: function(n, o) {
            if (n) {
                this.copyOptions['trainingLevel'] = [];
                this.copyOptions['trainingCategory'] = [];
                this.copyOptions['degreeType'] = [];
                this.copyOptions['formLearning'] = [];
                this.copyOptions['schemes'] = [];
                this.initCopyOptions();
                this.initTrainingLevelOptions(this.projId);
            }
        }
    },
    created() {
        this.randomId = "courseDialogEvent" + new Date().getTime();
        // 计算table的height值
        const elMain = document.getElementsByClassName("el-main");
        if (elMain && elMain.length > 0) {
            this.tableHeight = elMain[0].clientHeight - 180;
        }
        this.getRole();
        // 接受选择课程的信息
        Bus.$on(this.randomId, (state, courses) => {
            this.showCourseDialog = state;
            if (!courses) return;
            const data = {
                courseCodes: _.map(courses, "code"),
                trainingId: this.templateId,
                labelId: this.currentRow.categoryId,
                type: this.dataType
            };
            if (this.inChange) data.changeStatus = "1";
            addCourse(data).then(res => {
                this.init();
            });
        });
    },
    beforeDestroy() {
        Bus.$off(this.randomId);
    },
    mounted() {
        this.init();
    },
    // created() {
    //     this.randomId = "courseDialogEvent" + new Date().getTime();
    //     // 计算table的height值
    //     const elMain = document.getElementsByClassName("el-main");
    //     if (elMain && elMain.length > 0) {
    //         this.tableHeight = elMain[0].clientHeight - 180;
    //     }
    //     // 接受选择课程的信息
    //     Bus.$on(this.randomId, (state, courses) => {
    //         this.showCourseDialog = state;
    //         if (!courses) return;
    //         const data = {
    //             courseCodes: _.map(courses, "code"),
    //             trainingId: this.templateId,
    //             labelId: this.currentRow.categoryId,
    //             type: this.dataType,
    //             changeStatus: '1'
    //         };
    //         addCourse(data).then(res => {
    //             this.init();
    //         });
    //     });
    // },
    methods: {
        getRole(){
            getPower().then((res) => {
                this.role = res.data.power;
            })
        },
        changeGroupName(value){
            
            if(value!==""){
                var str=this.divideModuleForm.remark; 
                if(str.substring(3,4)=="选"&this.divideModuleForm.type=="0"){
                    this.divideModuleForm.remark=str.substring(0,4)+this.$utils.numberToChinese(value);   
                }
            }
        },
        deleteGroup(id,row) {
            // if (row.status === "2" && this.dataType == 2) {
            //     this.$message({
            //         message: "从培养模板继承过来的课程不能删除分组",
            //         type: "warning"
            //     });
            //     return;
            // }
            this.$confirm("此操作将删除分组或模块, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                deleteCourseLabelRelGroup({ id: id })
                    .then(res => {
                        if (res.code == 200) {
                            this.$message({
                                type: "success",
                                message: "删除成功"
                            });
                            this.init();
                        }
                    })
                    .catch(err => {
                        this.$message({
                            type: "error",
                            message: "删除失败"
                        });
                    });
            });
        },
        pickCourse(row, keyId, index, level) {
            if (event.target.checked && this.canCheck(keyId, index, level)) {
                this.curCheck.push(row);
            } else {
                if (this.curCheck.indexOf(row) >= 0) {
                    this.curCheck.splice(this.curCheck.indexOf(row), 1);
                }
                this.courseTables[index].isCourseCheck = false;
                this.courseTables[index].isModuleCheck = false;
            }
            if (this.curCheck.length === 0) {
                this.divideModuleForm.level = "";
            }
        },
        copyPre(row) {
            this.copyForm.categoryId = row.categoryId;
            this.copyData.labelId = row.categoryId;
            this.copyVisible = true;
            
        },
        copyPreAll(){
            this.copyVisibleAll = true;  
        },
        canCheck(keyId, index, level) {
            if (this.curCheck.length === 0) {
                this.divideModuleForm.level = level;
                return true;
            } else {
                if (
                    this.getParentId(this.curCheck[0].keyId) ===
                    this.getParentId(keyId)
                ) {
                    return true;
                } 
                //else {
                   // this.$message({
                     //   message: "请选择同一类型下的课程",
                     //   type: "warning"
                   // });
                    return true;
                //}
            }
        },
        getParentId(keyId) {
            return this.courseTables.filter(item => {
                return item.keyId == keyId;
            })[0].categoryId;
        },
        showDivideModule() {
            if (this.curCheck.length === 0) {
                this.$message({
                    message: "请选择要进行分组的课程或者模块",
                    type: "warning"
                });
                return;
            }
            let status = 0
            if (this.dataType == 2 && this.curCheck.length > 0) {
                this.curCheck.forEach(d=>{
                    if(d.status == 2){
                        status = 2
                    }
                })
            }
            // if (status == 2) {
            //     this.$message({
            //         message: "从培养模板继承过来的课程不能分组",
            //         type: "warning"
            //     });
            //     return;
            // }
            let checkedGroupMember=this.curCheck.filter((cur)=>{
                return cur.parentId
            });
            
            if(checkedGroupMember.length>0){
                this.divideModuleForm.parentId =checkedGroupMember[0].parentId;
                this.getGroupMember(checkedGroupMember[0].parentId);
            }else{
                this.divideModuleForm.parentId ='';
                this.setForm()
            };
            this.showDivideModuleDialog = true;
        },
        setForm() {
            const msg = `课程${this.$utils.numberToChinese(
                this.curCheck.length
            )}选一`;
            this.divideModuleForm.level = 2;
            this.divideModuleForm.remark = msg;
            this.divideModuleForm.totalNum = this.curCheck.length;
            this.divideModuleForm.chooseNum = this.curCheck.length;
        },
        writeDivideRelation(form) {
            if(this.divideModuleForm.expression == 1){
                this.divideModuleForm.maxNum = this.divideModuleForm.totalNum
            }else{
                this.divideModuleForm.maxNum = this.divideModuleForm.minNum
            }
            this.divideModuleForm.courseIdList = _.map(this.curCheck, "keyId");
            addCourseLabelRelGroup(this.divideModuleForm).then(res => {
                if (res.code === 200) {
                    this.$message({
                        message: "操作成功",
                        type: "success"
                    });
                    this.$refs[form].resetFields();
                    this.resetPage();
                    this.init();
                }
            });
        },
        // 获取课程分组组成员
        getGroupMember(parentId){
            getCourseLabelRelGroupMember(parentId,this.dataType).then(res=>{
                let member=res.data.courseLabelRelationList.map((item)=>{
                    return {
                        courseCode:item.courseCode,
                        courseName:item.name,
                        studyScore:item.credits,
                        studyTime:item.period,
                        remarks:item.remarks,
                        term:item.term,
                        keyId:item.id
                    }
                });
                let check=this.curCheck.filter((cur)=>{
                    return !cur.parentId
                });
                this.curCheck=check.concat(member);
                this.setForm()
            })
        },
        resetPage() {
            this.curCheck = [];
            this.showDivideModuleDialog = false;
        },
        init() {
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_KSLX"]
            };
            queryDic(data).then(res => {
                if (res.code === 200) {
                    this.loadOptions("types", res.data.X_KSLX);
                }
            });
            this.getCourseForm();
        },
        doubleReg(rule, value, callback) {
            const number = new Number(value);
            if (isNaN(number)) {
                callback(new Error("必须填写数字"));
            } else {
                callback();
            }
        },
        loadOptions(optionKey, dataObj) {
            for (const key in dataObj) {
                this.options[optionKey].push({
                    value: key,
                    label: dataObj[key]
                });
            }
        },
        backStep() {
            if (this.next) {
                this.next(-1);
            }
        },
        nextStep() {
            if (this.next) {
                this.next();
            }
        },
        add(row) {
            if(row.categoryId == 1070 || row.categoryId == 1581){
                this.$confirm("硕士只能选择本科课程，博士可选硕士和本科课程", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                .then(() => {
                    this.currentRow = row;
                    this.showCourseDialog = true;
                    this.$refs.courseDialog.queryCourseForm.isElective = this.currentRow.isElective;
                    this.$refs.courseDialog.queryCourseForm.enabled = 1;
                    this.$refs.courseDialog.onSubmit(row.categoryId);
                })
            }else{
                this.currentRow = row;
                this.showCourseDialog = true;
                this.$refs.courseDialog.queryCourseForm.isElective = this.currentRow.isElective;
                this.$refs.courseDialog.queryCourseForm.enabled = 1;
                this.$refs.courseDialog.onSubmit();
            }
        },
        edit(row) {
            // if (row.status === "2") {
            //     this.$message({
            //         message: "从培养模板继承过来的课程不能编辑",
            //         type: "warning"
            //     });
            //     return;
            // }
            this.currentRow = row;
            row.isEdit = true;
        },
        remove(row) {
            if (row.courseGroupId !== "" || row.parentId !== "") {
                this.$message({
                    message: "已经分组或者分模块的课程不能删除",
                    type: "warning"
                });
                return;
            }
            if (row.status === "2") {
                this.$message({
                    message: "从培养模板继承过来的课程不能删除",
                    type: "warning"
                });
                return;
            }
            this.$confirm("是否删除该课程?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                deleteCourse(row.keyId).then(response => {
                    if (response.code === 200) {
                        const courses = this.courseTables.filter(
                            c => c.index !== row.index
                        );
                        this.setArrayIndex(courses);
                        this.courseTables = courses;
                        this.$message({
                            type: "success",
                            message: "删除成功!"
                        });
                        this.init();
                    }
                });
            });
        },
        // 转换原生数据为渲染数据
        convert() {
            const viewData = [];
            this.appendCategory(viewData, "0");
            this.setArrayIndex(viewData);
            this.courseTables = viewData;
            this.spanDataForGroup = this.getspanData(2);
        },
        // 添加分类项
        appendCategory(viewData, courseCategoryPid) {
            const categories = this.rowData.courseCategories.filter(
                c => c.pid === courseCategoryPid
            );
            categories.forEach((c, index) => {
                // 判断是否存在子分类
                const childCategory = this.rowData.courseCategories.find(
                    c_ => c_.pid === c.id
                );
                const category = {
                    sequence:
                        courseCategoryPid === 0
                            ? this.getLargeFormat(index + 1)
                            : "",
                    categoryName: c.name,
                    categoryNameEn: c.nameEn,
                    isCategoryRow: true,
                    isElective: c.isElective, // 是否公共选修
                    showDivideBtn: !childCategory && this.hasCourse(c.id),
                    xid: c.id,
                    changeStatus: c.changeStatus
                };
                viewData.push(category);
                if (childCategory) {
                    this.appendCategory(viewData, c.id);
                } else {
                    this.appendCourse(viewData, c.id, c.isElective);
                }
            });
        },
        /** 是否有课程 */
        hasCourse(labelId) {
            return this.rowData.courses.some(i => {
                return i.pid === labelId;
            });
        },
        // 添加课程项
        appendCourse(viewData, courseCategoryId, isElective = "") {
            this.rowData.courses.sort(this.courseSort('parentId')).forEach(course => {
                if (course.pid === courseCategoryId) {
                    const row = {
                        ...course,
                        isCourseRow: true,
                        courseName: course.name,
                        courseNameEn: course.nameEn,
                        courseCode: course.code,
                        type: course.type,
                        isEdit: false,
                        studyScore: course.studyScore,
                        studyTime: course.studyTime,
                        pcTime: course.pcTime,
                        experimentTime: course.experimentTime,
                        remarks: course.remarks,
                        typeName: this.getExamModeTextByValue(course.type),
                        keyId: course.keyId,
                        categoryId: courseCategoryId,
                        courseId: course.courseId,
                        courseGroupId: course.courseGroupId,
                        parentId: course.parentId,
                        remark: course.remark,
                        parentRemark: course.parentRemark,
                        isElective: isElective, // //是否公共选修,
                        term: course.term,
                        compulsory: course.compulsory,
                        isCourseCheck: false,
                        isModuleCheck: false,
                        status: course.status,
                        changeStatus: course.changeStatus,
                        weekHour: course.weekHour,
                        weeks: course.weeks,
                        trainingLevelI18n: course.trainingLevelI18n
                    };
                    viewData.push(row);
                }
            });
            viewData.push({
                isAddRow: true,
                categoryId: courseCategoryId,
                isElective: isElective
            });
        },
        courseSort(params){
            return (a,b)=>{
                const val1=a[params];
                const val2=b[params];
                return val2-val1;
            }
        },
        // 根据小写数字获取大写数字
        getLargeFormat(num, suffix = "、") {
            const value = Number(num);
            if (!isNaN(value)) {
                let result = "";
                const char = [
                    "一",
                    "二",
                    "三",
                    "四",
                    "五",
                    "六",
                    "七",
                    "八",
                    "九"
                ];
                // const prev = ['十', '百']
                if (value > 0 && value < 10) {
                    result = char[value - 1];
                } else if (value >= 10 && value < 100) {
                    if (Math.floor(value / 10) === 1) {
                        result += "十";
                    } else {
                        result += char[Math.floor(value / 10) - 1] + "十";
                    }
                    if (value % 10 > 0) {
                        result += char[(value % 10) - 1];
                    }
                }
                return result + suffix;
            } else {
                return "";
            }
        },
        // 合并单元计算
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (row.isCategoryRow) {
                return {
                    rowspan: 1,
                    colspan: this.isRead ? 8 : 9 // 根据操作列是否可见，计算课程分类合并列
                };
            }
            if (columnIndex === 6) {
                const spanData = this.spanDataForGroup;
                const d = spanData.filter(ele => {
                    return ele.rowIndex === rowIndex;
                });
                if (d.length > 0) {
                    return {
                        rowspan: d[0].rowspan,
                        colspan: 1
                    };
                } else {
                    return {
                        rowspan: 1,
                        colspan: 1
                    };
                }
            } else {
                return {
                    rowspan: 1,
                    colspan: 1
                };
            }
        },
        getspanData(type) {
            // type==1 分模块, type ==2 分组
            let curPid = -1;
            let curRowIndex;
            let rowspan;
            const spanData = [];
            this.courseTables.forEach((ele, i, arr) => {
                if (ele.isCourseRow && ele["parentId"]) {
                    if (ele["parentId"] != curPid) {
                        if (curPid !== -1) {
                            spanData.push({
                                rowIndex: curRowIndex,
                                rowspan: rowspan
                            });
                        }
                        curPid = ele["parentId"];
                        curRowIndex = i;
                        rowspan = 1;
                    } else {
                        spanData.push({
                            rowIndex: i,
                            rowspan: 0
                        });
                        rowspan++;
                    }
                }
            });
            spanData.push({
                rowIndex: curRowIndex,
                rowspan: rowspan
            });
            return spanData;
        },
        // 获取后台相关课程数据
        getCourseForm() {
            this.rowData = {};
            this.rowData.courseCategories = [];
            courseLabelRelation(this.templateId, this.dataType).then(
                response => {
                    if (response.code === 200) {
                        this.trainingLevel = response.data.trainingLevel;
                        this.rowData.courseCategories = response.data.coursesLabelList.map(
                            c => {
                                return {
                                    id: c.labelId,
                                    name: c.label,
                                    nameEn: c.labelEn,
                                    pid: c.parentId,
                                    isElective: c.isElective
                                };
                            }
                        );
                        this.rowData.courses = response.data.courseLabelRelationList.map(
                            c => {
                                return {
                                    ...c,
                                    code: c.code, // 课程编码
                                    type: c.examMode,
                                    studyScore: c.credits,
                                    studyTime: c.period,
                                    pcTime: c.computeTime,
                                    experimentTime: c.experimentTime,
                                    remarks: c.remarks,
                                    remarksEn: c.remarksEn,
                                    name: c.name, // 课程名称
                                    nameEn: c.nameEn, // 课程名称
                                    pid: c.labelId, // 父目录Id
                                    keyId: c.id, // 主键Id，用于修改和删除
                                    courseId: c.courseId,
                                    courseGroupId:
                                        c.levelType == 2 ? "" : c.courseGroupId,
                                    parentId:
                                        c.levelType == 2
                                            ? c.courseGroupId
                                            : c.parentId,
                                    remark: c.remark,
                                    parentRemark: c.parentRemark,
                                    term: c.term,
                                    compulsory: c.compulsory,
                                    status: c.status,
                                    changeStatus: c.changeStatus,
                                    weekHour:c.weekHour,
                                    weeks:c.weeks,
                                    trainingI18n:c.trainingI18n
                                };
                            }
                        );
                        this.convert();
                    }
                }
            );
        },
        // 数组每个元素添加index属性
        setArrayIndex(data) {
            data.forEach((row, index) => {
                row.index = index;
            });
        },
        // 重置表单
        resetForm() {
            Object.keys(this.form).forEach(key => {
                this.form[key] = "";
                // if (/^s{1}\d{1,2}$/.test(key)) {
                //   this.form[key] = 0;
                // }
            });
            this.form.studyTime = 0;
        },
        closeSaveDialog() {
            this.showSaveDialog = false;
        },
        // 保存课程信息
        confirmSaveDialog(row) {
            const data = {
                courseCode: row.courseCode,
                trainingId: this.templateId,
                labelId: this.currentRow.categoryId,
                type: this.dataType,
                examMode: row.type,
                credits: row.studyScore,
                compulsory: row.compulsorys ? row.compulsorys : row.compulsory,
                experimentTime: row.experimentTime,
                computeTime: row.pcTime,
                weeks: row.studyTime,
                remarks: row.remarks,
                remarksEn: "",
                //term: row.term
            };
            if (this.inChange) data.changeStatus = "2";
            // 判断是添加还是编辑
            if (this.currentRow.isAddRow) {
                addCourse(data).then(response => {
                    if (response.code === 200) {
                        this.showSaveDialog = false;
                        this.form.keyId = response.data;
                        const obj = {};
                        this.setAttrValue(obj, this.form);
                        obj.isCourseRow = true;
                        obj.categoryId = this.currentRow.categoryId;
                        // 插入新行
                        let array = _.clone(this.courseTables);
                        array = array.slice(0, this.currentRow.index);
                        array = array.concat(
                            obj,
                            this.courseTables.slice(this.currentRow.index)
                        );
                        this.setArrayIndex(array);
                        this.courseTables = array;
                        this.init();
                    }
                });
            } else {
                data.id = row.keyId;
                data.courseId = row.courseId;
                EditCourse(data).then(response => {
                    if (response.code === 200) {
                        row.isEdit = false;
                        this.$message({
                            message: "操作成功",
                            type: "success"
                        });
                        this.init();
                    }
                });
            }
        },
        confirmCancelDialog(row) {
            this.init()
            row.isEdit = false;
        },
        isNumber(value, isNotEmpty = false) {
            if (isNotEmpty && value.toString().trim() === "") {
                return false;
            }
            if (isNaN(Number(value))) {
                return false;
            }
            return true;
        },
        getExamModeTextByValue(value) {
            if (this.options.types.find(t => t.value === value)) {
                return this.options.types.find(t => t.value === value).label;
            } else {
                return "";
            }
        },
        setAttrValue(target, src) {
            Object.keys(src).forEach(key => {
                target[key] = src[key];
            });
        },
        examModechange(value) {
            this.form.typeName = this.getExamModeTextByValue(value);
        },
        openCourseDialog() {
            this.showCourseDialog = true;
            // this.$refs.courseDialog.queryCourseForm.isElective = this.currentRow.isElective;
            delete this.$refs.courseDialog.queryCourseForm.isElective;
            this.$refs.courseDialog.queryCourseForm.enabled = 1;
            this.$refs.courseDialog.onSubmit();
        },
        // 计算总学时
        calcStudyTime() {
            let total = 0;
            for (let i = 1; i <= 10; i++) {
                total += +this.form["s" + i];
            }
            this.form.studyTime = total * 17;
        },
        resetFormValidate() {
            // 清除表单校验
            this.$refs.courseForm.resetFields();
        },
        // 搜索远程课程列表
        selectCourses(query) {
            this.courseCodes.length = 0;
            getCourseList({
                trainingLevel: "",
                formLearning: "",
                studentAbroad: "",
                condition: query,
                isElective: this.currentRow.isElective,
                pageSize_: 20,
                pageNum_: 1,
                total_: 0
            }).then(response => {
                if (response.code === 200) {
                    for (var i = 0; i < response.data.list.length; i++) {
                        this.courseCodes.push({
                            label: response.data.list[i].name,
                            value: response.data.list[i].code,
                            id: response.data.list[i].id
                        });
                    }
                }
            });
        },
        // 远程搜索课程选中
        changeSelectCourse(val) {
            const option = this.courseCodes.find(
                option => option.value === val
            );
            this.form.courseId = option.id;
            this.form.courseName = option.label;
            this.form.courseCode = option.value;
        },
        handleInitScheme() {
            if (!this.copyForm.year) {
                return;
            }
            this.copyData.year = this.copyForm.year;
            const self = this;
            const param = {
                id: this.$route.params.id,
                grade: this.copyForm.year,
                trainingLevel: this.copyForm.trainingLevel,
                trainingCategory: this.copyForm.trainingCategory,
                degreeType: this.copyForm.degreeType,
                formLearning: this.copyForm.formLearning
            };
            self.copyOptions.schemes = [];
            self.copyOptions.chapters = [];
            self.copyForm.schemeId = "";
            self.copyForm.categoryId = "";
            findCultureSchemeListByYear(param).then(res => {
                if (res && res.code === 200) {
                    _.forEach(res.data, item => {
                        self.copyOptions.schemes.push({
                            label: item.name + '(' + item.major + ')',
                            value: item.id
                        });
                    });
                }
            });
        },
        handleChangScheme(val) {
            const self = this;
            this.copyData.templateId = val;
            courseLabelRelation(val, 2).then(res => {
                if (res && res.code === 200) {
                    _.forEach(res.data.coursesLabelList, item => {
                        self.copyOptions.categorys.push({
                            label: item.label,
                            value: item.labelId
                        });
                    });
                }
            });
        },
        handleSubmitCopy() {
            // 数据校验
            this.$refs.copyForm.validate(valid => {
                if (valid) {
                    copyBackCourseLabelRelation(this.copyData).then(res => {
                        if (res && res.code === 200) {
                            this.copyVisible = false;
                        }
                        this.init();
                    });         
                }
            })
        },

        handleSubmitCopyAll() {
            // 数据校验
            this.$refs.copyForm.validate(valid => {
                if (valid) {
                    copyBackAllCourseLabelRelation(this.copyData).then(res => {
                        if (res && res.code === 200) {
                            this.copyVisibleAll = false;
                        }
                        this.init();
                    });         
                }
            })
        },
        resetCopyForm() {
            Object.keys(this.copyForm).forEach(key => {
                this.copyForm[key] = '';
            });
            this.copyForm.trainingCategory = [];
        },
        initCopyOptions() {
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: [
                    "K_XXXS",
                    "X_PYLB",
                    "X_XWLX"
                ]
            };
            queryDic(data)
                .then(res => {
                    if (res.code === 200) {
                        // this.fillSelect(res.data);
                        UTILS.fillSelect(
                            {
                                formLearning: "K_XXXS",
                                degreeType: "X_XWLX",
                                // trainingCategory: "X_PYLB"
                            },
                            res.data,
                            this.copyOptions
                        );
                        let params = {
                            lang: this.$store.getters.language,
                            keys: ["X_PYLB"]
                        };
                        sortTrainingCategories(params).then((res) => {
                            this.copyOptions.trainingCategory = res.data.X_PYLB;
                        });
                    }
                })
        },
        initTrainingLevelOptions(projId) {
            findTrainingLevelByProjId(projId).then(res => {
                if (res.code === 200) {
                    this.copyForm.trainingLevel = "";
                    this.loadTrainingLevelOptions("trainingLevel", res.data);
                }
            });
        },
        loadTrainingLevelOptions(optionKey, dataObj) {
            this.copyOptions[optionKey] = [];
            _.each(dataObj, level => {
                this.copyOptions[optionKey].push({
                    label:
                        "en" === this.$store.getters.language
                            ? level.nameEN
                            : level.nameCN,
                    value: level.code
                });
            });
        },
        showCourseDetail(c) {
            this.couseDetail = {
                ...c,
                code: c.courseCode,
                name: c.courseName,
                weeks: c.weeks,
                week_hour: c.weekHour,
                period: c.studyTime,
                credits: c.studyScore,
                trainingLevelI18n: c.trainingLevelI18n
            };
            this.showCourseDetailDialog = true;
        },
        handlerCloseCourseDialog() {
            this.showCourseDetailDialog = false;
        }
    }
};
</script>
<style lang="scss" scope>
    sort-col {
        width: 80px;
    }
    long-col {
        width: 120px;
    }
</style>
